/*
 * @Author: huangrongji
 * @Date: 2021-02-01 19:58:02
 * LastEditors: 胡皇
 * LastEditTime: 2021-03-28 18:20:51
 * @Description: file content
 * @FilePath: \news_testd:\工作空间\workspace\repair\pages\repairProgressPages\Modifyinformation\Modifyinformation.js
 */
// pages/Modifyinformation/Modifyinformation.js
const COS = require('../../../utils/cos-wx-sdk-v5.js');
const cos = new COS({
    SecretId: 'AKIDYyUjwt34oH1U9YxZ9i13oIU93gDTY9ll',
    SecretKey: 'qQi012oQYOzILccMpKZxYuXtNbYFfpjN'
});
const app = getApp();
const $api = require('../../../service/api').API;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: app.globalData.imgUrl,
        list: [],
        value1: 0,
        str_orderid: 0,
        fileList: [
            {
                url:'',
				deletable: true,
			},
        ],
        price: 0,
        radio: '1',
        array: [],
        index: 0,
        username: '',
        address: '',
        wechat: '',
        phone: '',
        type: 0,
        wt_id: 1,
        content: '',
        style: ''

    },
    uploadimg() {
        var _this = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original'], // 可以指定是原图还是压缩图，默认用原图
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                let filePath = res.tempFiles[0].path;
                let filename = filePath.substr(filePath.lastIndexOf('/') + 1);
                _this.setData({
                    ['fileList.[' + 0 + '].url']: filePath
                });

                cos.postObject({
                        Bucket: 'repair-1259727252',
                        Region: 'ap-guangzhou',
                        Key: filename,
                        FilePath: filePath,
                        onProgress: function (info) {
                            console.log(JSON.stringify(info));
                        }
                    },
                    function (err, data) {
                        console.log(err || data.Location);
                        wx.setStorageSync('imgUrl', 'https://' + data.Location);
                     
                    }
                );
            }
        });
    },

    // onChange(event) {
    //     // event.detail 为当前输入的值
    //     console.log(event.detail);
    //     this.setData({
    //         radio: event.detail
    //     });
    // },
     getText(e) {
        let str=e.detail.value;
		if(str.length<4){
			wx.showToast({
			  title: '详情不能少于4个字符',
			  icon:'none',
			  duration:2000
			})
			return false;
		}
		this.setData({
			content: e.detail.value
		});
     },
    onChangeInput(e) {
        let str=e.detail.value;
		let name=e.currentTarget.dataset.prop;
		if(name=='username'){
			if(str.length<2){
				wx.showToast({
				icon:'none',
				title: '报修人不能少于2位字符',
				duration:2000
				})
				return false;
			}
		}else
		if(name=='address'){
			if(str.length<4){
				wx.showToast({
				icon:'none',
				title: '宿舍地址不能少于4位字符',
				duration:2000
				})
				return false;
			}
		}
		else
		if(name=='wechat'){
			if(str.length<6){
				wx.showToast({
				icon:'none',
				title: '微信号不能少于6位字符',
				duration:2000
				})
				return false;
			}
		}
		else
		if(name=='phone'){
			if(str.length<11){
				wx.showToast({
				icon:'none',
				title: '手机号不能少于11位字符',
				duration:2000
				})
				return false;
			}
		}
		
		
		console.log(e.detail.value); //input 的 value
		console.log(e.currentTarget.dataset.prop); //data-prop 绑定的字符串，以此来确定改变的是哪个变量
		this.setData({
			[e.currentTarget.dataset.prop]: e.detail.value
		});
	},
    onSubmin() {
        let that=this;
		if(that.data.content.length>=4&&that.data.phone.length==11&&that.data.wechat.length>=6&&that.data.username.length>=2){
        const data = {
            orderid: this.data.str_orderid,
            style: this.data.style,
            name: this.data.username,
            address: this.data.address,
            wx: this.data.wechat,
            phone: this.data.phone,
            imageUrl: wx.getStorageSync('imgUrl'),
            content: this.data.content,
            openid: wx.getStorageSync('openid'),
            token: wx.getStorageSync('token')
        };
        console.log(data);
        $api.updateOrder(data).then((res) => {
            console.log(res);
            wx.showToast({
				title: res.message,
				icon: 'success',
				duration: 2000
			});
            if (res.code == 11) {
                wx.navigateTo({
                    url: '../FeedBackSuccess/feedBackSuccess',
                })
            }
        });
        wx.requestSubscribeMessage({
              tmplIds: ['RAzc1IgVnm6CZmKhiZIcHatVjTq0aJxdTO3MPuFO08Y'],
            })
        }else{
            wx.showToast({
                title: '请检查各信息字符数',
                icon:'none'
              })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let str_orderid = options.str_orderid;
        let that = this;
        that.setData({
            str_orderid: str_orderid
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        let that = this;
        $api.getAllorder(wx.getStorageSync('openid')).then((res) => {
            console.log(res);

            var list = [];
            let i = 0;
            var styles = 0,names,addresss,wxs;
            for (; i < res.data.length; i++) {
                if (res.data[i].str_orderid == that.data.str_orderid) {
                    list.push(res.data[i]);
                    console.log(list[0]);
                    styles = res.data[i].style;
                    if (list.style == 1) {
                        list[0].style = "上门预约";
                    } else {
                        list[0].style = "到店预约";
                    }
                }
            }

            that.setData({
                list: list,
                style: styles,
                imgUrl:list[0].imgUrl,
                username:list[0].name ,
                address: list[0].address,
                wechat: list[0].wx,
                content:list[0].content,
                phone:list[0].phone
            });



        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {}
});